const app = getApp();
var share_count = 0;
var api = require('../../api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_attr: false,
    message: false,
    page:0,
    nav_icon_list:[
      {
        type:"icon-recommend",
        name:"推荐",
        backgroundColor:"#E80111",
        open_type:"navigate",
        url:"../recommend/recommend",
      },
      {
        type: "icon-course",
        name: "课程",
        backgroundColor:"#910A81",
        open_type:"navigate",
        url:"../course/course",
      },
      {
        type: "icon-audio",
        name: "音频",
        backgroundColor: "#1E2187",
        open_type:"navigate",
        url:"../audio/audio",
      }
    ]
    
  },
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var bind_id = wx.getStorageSync("bind_id");

    if (bind_id){

      app.bind(bind_id);

    }else{

      app.bind();

    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // app.login();
    this.getDetail();
    var equipmentSystem = wx.getStorageSync('equipmentSystem');
    console.log(equipmentSystem)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading(); 

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading(); 

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    
   
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url+"?user_id=" + user_id,
      title: '得道1314'
    };

  },
  
  //获取首页
  getDetail:function(){
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that=this;
    wx.request({
      url: api.index.index,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        page:that.data.page
      },

      success: function (res) {
       console.log(res);
       if(res.data.code == 0){
         that.setData({
           bannerList: res.data.data.banner_list,
           freeList: res.data.data.freeSectionList,
           lessonList: res.data.data.lesson_list,
           filmList:res.data.data.film_section,
           film_auth: res.data.data.film_auth,
         });
         console.log(that.data.freeList)
       } else if (res.data.code == -1){
        //  app.login();
         wx.navigateTo({
           url: '/pages/grant/grant',
         })
       }
       console.log(that.data.bannerList)
      }
    });
    
  },

  

  //跳转到免费专栏列表
  toFree:function(e){
    console.log(e);
    
    var index = e.currentTarget.dataset.index;
    var proList = this.data.freeList;
    console.log(proList)
    var name = proList[index].name;
    let str = JSON.stringify(proList[index]);
    console.log(str);
    
    wx.navigateTo({
      url: '/pages/free/free?str=' + str,
    })
  },

  //跳转到免费专栏详情
  toFreeDetail: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var proList = this.data.free_list;
    var name = proList[index].name;
    let str = JSON.stringify(proList[index]);
    console.log(str);
    wx.navigateTo({
      url: '/pages/freeDetail/freeDetail?str=' + str,
    })
  },

  //跳转到课程专栏列表
  toCourse: function (e) {
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var lessonList = page.data.lessonList;
    var lessonId = lessonList[index].id;
    let str = JSON.stringify(lessonList[index]);
    console.log(str);
    console.log("————————————————测试————————————————");
    console.log(lessonId);
    wx.navigateTo({
      url: '../../pages/classColumn/classColumn?lessonId=' + lessonId,
    })
  },

  //跳转到专栏
  toColumnList:function(){
    wx.navigateTo({
      url: '../../pages/coulumnList/coulumnList',
    })
  },

  //跳转到影视
  toMoviesList: function () {
    if(this.data.film_auth == 1){
      wx.navigateTo({
        url: '../../pages/moviesList/moviesList',
      })
    }
    
  },

  //跳转到影视专栏列表
  toMovies:function(e){
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var filmList = page.data.filmList;
    var lessonId = filmList[index].id;
    let str = JSON.stringify(filmList[index]);
    console.log(str);
    if(page.data.film_auth == 1){
      wx.navigateTo({
        url: '../../pages/movies/movies?lessonId=' + lessonId,
      })
    }


    
  },

  //影视提示框
  doMessage: function () {
    if(this.data.film_auth != 1){
      this.setData({
        show_attr: true,
        message: true,
      });
    }
  },

  //点击弹窗退出
  clear: function () {
    var page = this;
    page.setData({
      show_attr: false,
      message: false,
    })

  },

  //banner跳转
  navigatorClick: function (res){
    console.log(res);
    var page = this;
    var bannerList = page.data.bannerList;
    console.log(bannerList)
  }
})